package com.netty.code.self.resp.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;

/**
 * TODO
 * @author wuzz
 * @since 2024/7/11
 */
public class NioClient {


    public static void main(String[] args) throws IOException {

        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(false);

        if (!socketChannel.connect(new InetSocketAddress("127.0.0.1", 8888))) {
            while (!socketChannel.finishConnect()) {
                System.out.println("连接需要耗时，，客户端不会阻塞");
            }
        }

        String str = "hello";
//        ByteBuffer buffer = ByteBuffer.wrap(str.getBytes(StandardCharsets.UTF_8));
        ByteBuffer buffer = ByteBuffer.allocate(5);
        buffer.put(str.getBytes(StandardCharsets.UTF_8));
        buffer.flip();  // 重中之重
        socketChannel.write(buffer);
        System.in.read();

    }

}
